package com.zhhs.galaxyott.activity;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.adapters.SpecialsViewPagersAdapter;
import com.zhhs.galaxyott.beans.MoreSpecialBean;
import com.zhhs.galaxyott.beans.MoreSpecialTypeBean;
import com.zhhs.galaxyott.http.api.RequestMaker;
import com.zhhs.galaxyott.httpVolley.base.ResponseListener;
import com.zhhs.galaxyott.httpVolley.response.MoreSpecialTypeResponse;
import com.zhhs.galaxyott.utils.Logs;

/**
 * 
 * @author lc
 * @created 2016年6月29日 注：此Activity已被替代
 */
public class MoreSpecialActivity extends BaseActivity implements OnClickListener {

    private static String TAG = DeviceCenterActivity.class.getSimpleName();

    private ImageView type_img;
    private LinearLayout Tag_specials;
    private ImageView page_item_0, page_item_1, page_item_2, page_item_3;
    private TextView new_item_0_txt, new_item_1_txt, new_item_2_txt, new_item_3_txt;

    private ViewPager mViewPager;
    private SpecialsViewPagersAdapter mPagerAdapter;

    private List<View> mViews;
    private List<MoreSpecialTypeBean> FirsttypeData;
    private List<MoreSpecialTypeBean> typeData;
    private List<MoreSpecialBean> listData;

    private View PageView0, PageView1, PageView2, PageView3;

    private Toast toastTest;

    public static int pageItem;

    @Override
    protected void onCreateView() {
        setContentView(R.layout.activity_morespecial);

        pageItem = 0;

        type_img = (ImageView) findViewById(R.id.type_img);
        type_img.setBackgroundResource(R.drawable.morespecial_img);

        toastTest = new Toast(getApplicationContext());

        FirsttypeData = new ArrayList<MoreSpecialTypeBean>();
        typeData = new ArrayList<MoreSpecialTypeBean>();
        listData = new ArrayList<MoreSpecialBean>();

        initView();

        loadData();

    }

    private void loadData() {
        RequestMaker.getMoreSpecialType(this, new ResponseListener<MoreSpecialTypeResponse>() {

            @Override
            public void onResponse(MoreSpecialTypeResponse result) {
                if (result.mMoreSpecialTypeBeans == null || result.mMoreSpecialTypeBeans.size() == 0) {
                    return;
                }

                Logs.logD("DEBUG_DT", "THIS IS getMoreSpecialType" + result.mMoreSpecialTypeBeans.size());
                if (result.errCode == 0) {

                    FirsttypeData = result.mMoreSpecialTypeBeans;

                    if (FirsttypeData.size() > 4) {
                        typeData = FirsttypeData.subList(0, 4);
                    } else {
                        typeData = FirsttypeData;
                    }
                    for (int i = 0; i < typeData.size(); i++) {
                        getTitleLayout(i).setVisibility(View.VISIBLE);
                    }
                    setText(typeData);
                    initViewPagers();
                }
            }

            @Override
            public void onError(VolleyError error) {
                Logs.logD("DEBUG_DT", "THIS IS getMoreSpecialType-->ERR");
            }
        });
    }

    private FrameLayout getTitleLayout(int i) {
        return (FrameLayout) Tag_specials.getChildAt(i * 2 + 1);
    }

    private TextView getTextView(int i) {
        return ((TextView) ((FrameLayout) Tag_specials.getChildAt(i * 2 + 1)).getChildAt(1));
    }

    private void setText(List<MoreSpecialTypeBean> data) {

        for (int i = 0; i < data.size(); i++) {
            getTextView(i).setText(data.get(i).getTypeTitle());

        }
    }

    private void initView() {
        Tag_specials = (LinearLayout) findViewById(R.id.Tag_specials);

        page_item_0 = (ImageView) findViewById(R.id.new_item_1);
        page_item_1 = (ImageView) findViewById(R.id.action_item_2);
        page_item_2 = (ImageView) findViewById(R.id.about_item_3);
        page_item_3 = (ImageView) findViewById(R.id.discount_item_4);
        page_item_0.requestFocus();

        new_item_0_txt = (TextView) findViewById(R.id.new_item_1_txt);
        new_item_1_txt = (TextView) findViewById(R.id.new_item_2_txt);
        new_item_2_txt = (TextView) findViewById(R.id.new_item_3_txt);
        new_item_3_txt = (TextView) findViewById(R.id.new_item_4_txt);
        new_item_0_txt.setBackgroundResource(R.drawable.specials_title_current_bg);

        page_item_0.setOnFocusChangeListener(mOnFocusChangeListener);
        page_item_1.setOnFocusChangeListener(mOnFocusChangeListener);
        page_item_2.setOnFocusChangeListener(mOnFocusChangeListener);
        page_item_3.setOnFocusChangeListener(mOnFocusChangeListener);

        mViewPager = (ViewPager) findViewById(R.id.special_viewpage);

    }

    private void initViewPagers() {

        PageView1 = getLayoutInflater().inflate(R.layout.viewpage_morespecial_item, null);

        mViews = new ArrayList<View>();
        for (int i = 0; i < typeData.size(); i++) {
            mViews.add(getLayoutInflater().inflate(R.layout.viewpage_morespecial_item, null));
        }

        mPagerAdapter = new SpecialsViewPagersAdapter(this, mViews, typeData);
        // mViewPager.setOffscreenPageLimit(0);

        mViewPager.setAdapter(mPagerAdapter);

        mViewPager.setOnPageChangeListener(mOnPageChangeListener);

    }

    OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int page) {

            Logs.logD("DEBUG_LC", "THIS IS onPageSelected--->" + page);
            pageItem = page;
            switch (page) {

            case 0:

                setFocusView(page_item_0, false);
                break;
            case 1:
                setFocusView(page_item_1, false);
                break;
            case 2:
                setFocusView(page_item_2, false);
                break;
            case 3:
                setFocusView(page_item_3, false);
                break;

            default:
                break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    };

    /**
     * 设置viewpager页面与title对应
     * 
     * @param view
     *            有焦点的控件
     * @param isFocus
     *            是否有焦点
     */
    private void setFocusView(View view, boolean isFocus) {

        // 注意焦点多次设置同一view的切换会出现闪屏现象
        if (view == page_item_0) {
            page_item_1.setFocusable(isFocus);
            page_item_2.setFocusable(isFocus);
            page_item_3.setFocusable(isFocus);
            if (!isFocus) {
                new_item_0_txt.setBackgroundResource(R.drawable.specials_title_current_bg);
                new_item_1_txt.setBackgroundResource(R.drawable.special_item);
                new_item_2_txt.setBackgroundResource(R.drawable.special_item);
                new_item_3_txt.setBackgroundResource(R.drawable.special_red_item);
            }

        }
        if (view == page_item_1) {
            page_item_0.setFocusable(isFocus);
            page_item_2.setFocusable(isFocus);
            page_item_3.setFocusable(isFocus);

            if (!isFocus) {
                new_item_1_txt.setBackgroundResource(R.drawable.specials_title_current_bg);
                new_item_0_txt.setBackgroundResource(R.drawable.special_item);
                new_item_2_txt.setBackgroundResource(R.drawable.special_item);
                new_item_3_txt.setBackgroundResource(R.drawable.special_red_item);
            }

        }
        if (view == page_item_2) {
            page_item_0.setFocusable(isFocus);
            page_item_1.setFocusable(isFocus);
            page_item_3.setFocusable(isFocus);

            if (!isFocus) {
                new_item_2_txt.setBackgroundResource(R.drawable.specials_title_current_bg);
                new_item_1_txt.setBackgroundResource(R.drawable.special_item);
                new_item_0_txt.setBackgroundResource(R.drawable.special_item);
                new_item_3_txt.setBackgroundResource(R.drawable.special_red_item);
            }

        }
        if (view == page_item_3) {
            page_item_0.setFocusable(isFocus);
            page_item_1.setFocusable(isFocus);
            page_item_2.setFocusable(isFocus);

            if (!isFocus) {
                new_item_3_txt.setBackgroundResource(R.drawable.specials_title_current_bg);
                new_item_1_txt.setBackgroundResource(R.drawable.special_item);
                new_item_2_txt.setBackgroundResource(R.drawable.special_item);
                new_item_0_txt.setBackgroundResource(R.drawable.special_item);
            }

        }

        view.setFocusable(true);

    }

    OnFocusChangeListener mOnFocusChangeListener = new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            Logs.logD("DEBUG_LC", "THIS IS onFocusChange-->");

            if (v.findFocus() == page_item_0) {

                pageItem = 0;
                mViewPager.setCurrentItem(0);
            }
            if (v.findFocus() == page_item_1) {

                pageItem = 1;
                mViewPager.setCurrentItem(1);
            }
            if (v.findFocus() == page_item_2) {

                pageItem = 2;
                mViewPager.setCurrentItem(2);
            }
            if (v.findFocus() == page_item_3) {

                pageItem = 3;
                mViewPager.setCurrentItem(3);
            }

        }

    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (Tag_specials.findFocus() != null)
                setFocusView(Tag_specials.findFocus(), false);

            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (Tag_specials.findFocus() != null)
                setFocusView(page_item_0, true);

            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (Tag_specials.findFocus() != null)
                setFocusView(page_item_0, true);

            break;

        default:
            break;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        default:
            break;
        }

    }

}
